package com.jackup.jasp;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jackup.jasp.internet.Login_utils;

import Utils.Waitting_dialog;
import base.Constant;
import user.Activity_Userpage;

public class Activity_Login extends AppCompatActivity implements View.OnClickListener{
    private Button login_btn;
    private EditText user_name_edt;
    private EditText user_pwd_edt;
    private String user_name;
    private String user_pwd;
    private Dialog dialog;
    private static final int error_message = Constant.ERROR_MSG;

    private static final String TAG = "Activity_Login";

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    Waitting_dialog.closeDialog(dialog);
                    Intent i = new Intent();
                    i.setClass(Activity_Login.this, Activity_Userpage.class);
                    startActivity(i);
                    Activity_Login.this.finish();
                    break;
                case 1:
                    Waitting_dialog.closeDialog(dialog);
                    Toast.makeText(Activity_Login.this,"账号密码有误",Toast.LENGTH_SHORT).show();
                    break;
                case -10086:
                    Waitting_dialog.closeDialog(dialog);
                    Toast.makeText(Activity_Login.this,"网络错误",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init(){
        login_btn = (Button) findViewById(R.id.login_btn);
        login_btn.setOnClickListener(this);
        login_btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        login_btn.setBackgroundResource(R.drawable.button_shape_press);
                        break;
                    case MotionEvent.ACTION_UP:
                        login_btn.setBackgroundResource(R.drawable.button_shape);
                        break;
                    default:
                        break;
                }
                return false;
            }
        });
        user_name_edt = (EditText) findViewById(R.id.user_name_edt);
        user_pwd_edt = (EditText) findViewById(R.id.user_pwd_edt);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_btn:
                user_name = user_name_edt.getText().toString();
                user_pwd = user_pwd_edt.getText().toString();
                dialog = Waitting_dialog.createLoadingDialog(Activity_Login.this);
                Login_utils.Login(user_name,user_pwd,handler);
                break;
            default:
                break;
        }
    }
}
